package vsm.vsm_server.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vsm.vsm_server.pojo.Notice.Notice;
import vsm.vsm_server.pojo.Result.Result;
import vsm.vsm_server.pojo.Result.ResultEnum;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.service.*;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/follow")
@CrossOrigin
public class RelationController {
    @Autowired
    private RelationService relationService;
    @Autowired
    private UserService userService;
    @Autowired
    private VodService vodService;
    @Autowired
    private NoticeService noticeService;

    public static RelationController rci;
    public RelationController(){
        rci = this;
    }
    /*
    根据id判断是否关注该用户
   */
    @GetMapping("/{userid}/{followid}")
    public Result<Integer> isFollow(@PathVariable BigInteger userid,@PathVariable BigInteger followid){
        log.info("查询用户{}与指定id：{}关系",userid,followid);
        if(Objects.equals(userid, followid))
            return Result.error_special(ResultEnum.USER_FOLLOW_IS_SELF);
        if(userService.getById(followid)!=null){
            if(userService.getById(userid)!=null ){
                Integer relation = relationService.isFollow(userid, followid);
                return Result.success(Objects.requireNonNullElse(relation, 0));
            }else{
                return Result.error_special(ResultEnum.USER_NOT_EXIST);
            }

        }else{
            return Result.error_special(ResultEnum.USER_FOLLOW_NOT_EXIST);
        }


    }


    /*
   根据id关注该用户
  */
    @PostMapping("/{userid}/{followid}")
    public Result<Integer> follow(@PathVariable BigInteger userid,@PathVariable BigInteger followid){
        log.info("用户{}根据指定id：{}关注用户",userid,followid);
        if(Objects.equals(userid, followid))
            return Result.error_special(ResultEnum.USER_FOLLOW_IS_SELF);
        if(userService.getById(followid)!=null){
            if(userService.getById(userid)!=null ){
                //检测是否已经关注
                Integer isFollowed = relationService.isFollow(userid,followid);

                if(isFollowed!=null){
                    return Result.error_special(ResultEnum.USER_FOLLOW_EXIST);
                }
                //检测该用户是否关注你
                Integer relation = relationService.isFollow(followid, userid);
                int state = 0;
                if(relation!=null){ //若关注
                    state = 2;
                    relationService.update(followid,userid,2);
                }else{ //未关注
                    state = 1;
                }
                relationService.follow(userid,followid,state);
                //增加订阅数
                userService.addSubs(userid);
                //增加粉丝数
                userService.addFans(followid);
                //保存记录
                Notice notice = new Notice();
                notice.setType(2);
                String datestr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                notice.setTime(datestr);
                notice.setUserId(followid);
                notice.setSenderId(userid);
                notice.setContent("关注了你");
                noticeService.saveNotice(notice);



                return Result.success(state);
               //return Result.error_special(ResultEnum.USER_FOLLOW_ERROR);

            }else{
                return Result.error_special(ResultEnum.USER_NOT_EXIST);
            }

        }else{
            return Result.error_special(ResultEnum.USER_FOLLOW_NOT_EXIST);
        }

    }
    /*
   根据id取消关注该用户
  */
    @DeleteMapping("/{userid}/{followid}")
    public Result<String> delete(@PathVariable BigInteger userid,@PathVariable BigInteger followid){
        log.info("用户{}根据指定id：{}取消关注用户",userid,followid);
        if(Objects.equals(userid, followid))
            return Result.error_special(ResultEnum.USER_FOLLOW_IS_SELF);
        if(userService.getById(followid)!=null){
            if(userService.getById(userid)!=null ){
                //检测该用户是否关注你
                Integer relation = relationService.isFollow(followid, userid);
                if(relation!=null)//若关注
                    relationService.update(followid,userid,1);
                relationService.delete(userid,followid);
                //减少订阅数
                userService.deSubs(userid);
                //减少粉丝数
                userService.deFans(followid);
                return Result.success();
            }else{
                return Result.error_special(ResultEnum.USER_NOT_EXIST);
            }

        }else{
            return Result.error_special(ResultEnum.USER_FOLLOW_NOT_EXIST);
        }

    }
    /*
    获取粉丝列表
     */
    @GetMapping("/getfans/{userid}")
    public Result<List<UserInfo>> getfans(@PathVariable BigInteger userid){
        List<BigInteger> idList = relationService.getFans(userid);
        List<UserInfo> userInfoList = new ArrayList<>();
        for(BigInteger i :idList){
            UserInfo userInfo = userService.getInfoById(i);

            if(userInfo.getAvatar()!=null){
                userInfo.setAvatar(vodService.getPicture(userInfo.getAvatar()));
            }
            userInfoList.add(userInfo);
        }
        return Result.success(userInfoList);
    }
     /*
    获取关注列表
     */
     @GetMapping("/getsubs/{userid}")
     public Result<List<UserInfo>> getsubs(@PathVariable BigInteger userid){
         List<BigInteger> idList = relationService.getSubs(userid);
         List<UserInfo> userInfoList = new ArrayList<>();
         for(BigInteger i :idList){
             UserInfo userInfo = userService.getInfoById(i);
             if(userInfo.getAvatar()!=null){
                 userInfo.setAvatar(vodService.getPicture(userInfo.getAvatar()));
             }
             userInfoList.add(userInfo);
         }
         return Result.success(userInfoList);
     }
}
